$(function() {

	$('#week-number').remove();
	var date = new Date();

	var fullYear = date.getFullYear();
	var month = date.getMonth() + 1;
	var day = date.getDate();
	// 本月第一天
	date.setDate(1);
	var firstDay = date.getDate();

	// 第一天周几
	var firstIndex = date.getDay();

	// 本月最后一天
	date.setMonth(date.getMonth() + 1, 1);
	date.setDate(date.getDate() - 1);
	var lastDay = date.getDate();

	// 最后一天周几
	var lastIndex = date.getDay();

	var rowNumber = 0;
	var html = '<div id="week-number" class="date-div">';
	for (var i = 0; i < firstIndex; i++) {
		html += '<div></div>';
		rowNumber++;
	}
	for (var i = firstDay; i <= lastDay; i++) {
		html += '<div class="number ';
		if (i == day) {
			html += ' today';
		}
		html += '">' + i + '</div>';
		rowNumber++;
		if (rowNumber % 7 == 0 && !(i == lastDay && lastIndex == 6)) {
			html += '</div><div id="week-number" class="date-div">';
		}
	}
	for (var i = 0; i < 6 - lastIndex; i++) {
		html += '<div></div>';
	}
	html += '</div>';

	$('#date-div').append(html);

	$('#week-number div.number').click(function() {
		var thisText = $(this).text();
		if (thisText != '') {
			if (thisText < 10) {
				thisText = '0' + thisText;
			}
			$('#now-date').text(fullYear + '-' + month + '-' + thisText);
		}
	});

	$('#data').click(function() {
		var clickDay = $('#now-date').text();

		chrome.tabs.query({
			active : true,
			currentWindow : true
		}, function(t) {
			chrome.tabs.sendMessage(t[0].id, {
				workDate : clickDay
			});
		});
	});
});
